export function photo(elm, opt) {
  var me = this;
  // me.container = typeof elm == 'object' ? elm : document.getElementById(elm);
  me.container = elm.container;
  me.childrens = me
    .container
    .getElementsByClassName(elm.childrens);
  // console.log(me.childrens);
  if (me.childrens && me.childrens.length > 0) {
    me.opt = {
      defaultHeight: 280,
      margin: parseInt(getComputedStyle(me.childrens[0])['margin-left']) + parseInt(getComputedStyle(me.childrens[0])['margin-right'])
    };
    for (let i in opt) {
      me.opt[i] = opt[i];
    }
    me.refleshLayout();
  }
};
photo.prototype = {
  _getEveryLineNums: function () {
    var me = this,
      l = me.childrens.length,
      curWidth = 0,
      start = 0,
      tmp = {},
      r = [];
    for (var i = 0; i < l; i++) {
      var o = me._imgMsg(me.childrens[i]);

      curWidth += me.opt.defaultHeight * o.w / o.h + me.opt.margin;
      if (curWidth >= me.maxWidth) {
        tmp = {
          start: start,
          end: i,
          height: Math.round(me.maxWidth / curWidth * me.opt.defaultHeight)
        };
        r.push(tmp);
        start = i + 1;
        curWidth = 0;
      }
    }
    return r;
  },
  _imgMsg: function (obj) {
    var o = {
      w: obj.getAttribute('data-width'),
      h: obj.getAttribute('data-height')
    };
    return o;
  },
  _sort: function (data) {

    var me = this,
      max = me.childrens.length - 1;
    if (data.length > 0) {
      for (var i in data) {
        me._setAttr(data[i].start, data[i].end, data[i].height, false)
      }
      if (data[i].end < max) {
        me._setAttr(data[i].end + 1, max, me.opt.defaultHeight, true)
      }
    } else {
      me._setAttr(0, max, me.opt.defaultHeight, true)
    }
  },
  _setAttr: function (start, end, h, last) {
    var me = this,
      item = me.childrens;
    var neW = me._getNeWidth(start, end, h, last, item);
    for (var i in neW) {
      me.__setAttrBox(neW[i], h, item[i]);
      me.__setAttrImg(neW[i], h, item[i]);
    }
  },
  __setAttrBox: function (w, h, box) {
    box.style.width = w - 1 + 'px'; //mx 同行每个box宽 -1   临时修改手段！！！
    box.style.height = h + 'px';
  },
  __setAttrImg: function (w, h, box) {
    var me = this;
    var img = box.getElementsByTagName('img')[0];
    var baseSize = me._imgMsg(box);

    if (w * h / baseSize.w > baseSize.h) {
      h = w * baseSize.h / baseSize.w;
    } else {
      w = baseSize.w * h / baseSize.h;
    }
    if (img) {
      img.width = w;
      img.height = h;
    }
  },
  _getNeWidth: function (s, e, h, last, item) {
    var me = this,
      o = {},
      r = [],
      tmpW = 0,
      tmpS,
      dValue = e > s
        ? e - s + 1
        : 1;
    for (var i = s; i <= e; i++) {
      o = me._imgMsg(item[i]);
      r[i] = h / o.h * o.w;
      tmpW += r[i];
    }
    var gep = !last
      ? Math.round((me.maxWidth - tmpW - me.opt.margin * dValue) / dValue)
      : 0;
    tmpW = 0;
    for (var i in r) {
      tmpS = Math.floor(r[i] + gep);
      if (tmpW + tmpS + me.opt.margin > me.maxWidth || (Number(i) === e && tmpW + tmpS + me.opt.margin <= me.maxWidth && !last)) {
        tmpS = me.maxWidth - tmpW - me.opt.margin;
      }
      r[i] = tmpS;
      tmpW += tmpS + me.opt.margin;
    }
    return r;
  },
  refleshLayout: function () {
    var me = this;
    me.maxWidth = me.container.offsetWidth;
    me._sort(me._getEveryLineNums());
  }
};
